<template>
  <div class="col main-content">
    <div class="row tool-bar" id="tool-bar">
      <div class="col">
        <div class="inline-item">
          <el-input v-model="search.name" clearable style="width: 200px;" placeholder="请输入查询信息"/>
          <el-button @click="searchData" icon="el-icon-search" type="primary">查询</el-button>
          <el-button type="primary" @click="addGood"><i class="fa fa-shopping-cart"></i> 添加商品</el-button>
        </div>
      </div>
    </div>

    <div class="row">
      <div class="col">
        <el-table
          :data="goods"
          border
          stripe
          tooltip-effect="dark"
          v-loading="loading"
          ref="table"
        >
          <el-table-column
            label="ID" width="80"
          >
            <template slot-scope="scope">
              <div class="center">
                <span>{{scope.row.id}}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="商品"
          >
            <template slot-scope="scope">
              <h-good-display v-model="scope.row"/>
            </template>
          </el-table-column>
          <el-table-column
            label="品牌"
          >
            <template slot-scope="scope">{{ scope.row.brand.name }}</template>
          </el-table-column>
          <el-table-column
            label="分类"
          >
            <template slot-scope="scope">{{ scope.row.category.namePath }}</template>
          </el-table-column>
          <el-table-column label="规格">
            <template slot-scope="scope">
              <h-good-specs-display v-model="scope.row"/>
            </template>
          </el-table-column>
          <el-table-column
            label="描述"
          >
            <template slot-scope="scope">{{ scope.row.description }}</template>
          </el-table-column>
          <el-table-column
            label="状态" width="90"
          >
            <template slot-scope="scope">
              <div v-if="scope.row.isShow==1" class="el-alert el-alert--success is-center">
                显示
              </div>
              <div v-else class="el-alert el-alert--error is-center">
                已隐藏
              </div>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <div class="center">
                <el-button type="text">
                  编辑
                </el-button>

                <el-button v-if="scope.row.isShow==0" type="text"
                           @click="changeStatus(scope.$index, scope.row)">
                  显示
                </el-button>
                <el-button v-else="scope.row.isShow==1" type="text"
                           @click="changeStatus(scope.$index, scope.row)">
                  隐藏
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="h-pagination">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="search.pagination.page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="search.pagination.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="search.pagination.total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>

  import AdminService from 'src/service/admin'
  import TableMix from 'src/components/table-mix'
  import HGoodSpecsDisplay from '../../common/display/HGoodSpecsDisplay'
  import HGoodDisplay from 'src/components/common/display/HGoodDiaplay'

  export default {
    name: 'AdminGood',
    components: {HGoodSpecsDisplay, HGoodDisplay},
    mixins: [TableMix],
    data() {
      return {
        goods: [],
        loading: false,
        search: {
          name: null,
          pagination: {
            total: 0,
            page: 1,
            limit: 10
          },
          orderBy: 'is_show desc, id'
        }
      }
    },
    filters: {
      imgsrc: function (data) {
        if (data != null) {
          if (data.imgs != null && data.imgs.length > 0) {
            return data.imgs[0]
          }
        }
        return '#'
      }
    },
    methods: {

      changeStatus: function (index, data) {
        const me = this;
        const message = data.isShow == 1 ? '此操作后将该商品将不会出现在商品列表中，是否继续?' : '此操作后将该商品将出现在商品列表中，是否继续?'
        me.$confirm(message, '提示', {
          confirmButtonText: '确定操作',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          AdminService.changeGoodShow({
            ids: [data.id],
            status: data.isShow == 1 ? 0 : 1
          }).then(ds => {
            if (ds != null) {
              data.isShow = status
              me.$message.success('修改状态成功!')
            }
          })
        }).catch(() => {
        })
      },
      handleSizeChange: function (size) {
        this.search.pagination.limit = size
        this.getData()
      },
      handleCurrentChange: function (page) {
        this.search.pagination.page = page
        this.getData()
      },
      searchData: function () {
        this.search.pagination.page = 1
        this.getData()
      },
      addGood: function () {
        this.$router.push('/admin/goods/good/create.html')
      },
      getData: function () {
        const me = this
        me.loading = true
        AdminService.searchGood(me.search).then(datas => {
          if (datas != null) {
            me.goods = datas.datas
            me.search.pagination = datas.pagination
          }
          me.loading = false
        })
      }
    },

    mounted: function () {
      this.getData()
    }
  }
</script>
